from sls_gc_missionslist_ui import Ui_MissionsList
from PyQt6.QtWidgets import QWidget,QApplication,QDialog
from PyQt6.QtGui import QBrush,QColor
from PyQt6.QtCore import Qt
import sys
from sls_gc_agent import sls_Agent
from sls_gc_MissionsTakeoff import gc_MisssionsTakeoff
from sls_gc_MissionsLand import gc_MisssionsLand
from sls_gc_MissionsWaypoint import gc_MissionsWaypoint
from sls_gc_MissionsQRLand import gc_MissionsQRLand
from sls_gc_MissionsPurePursuit import gc_MissionsPurePursuit

mission_table={"MissionsTakeoff":gc_MisssionsTakeoff,
                "MissionsLand":gc_MisssionsLand,
                "MissionsWaypoint":gc_MissionsWaypoint,
                "MissionsQRLand":gc_MissionsQRLand,
                "MissionsPurePursuit":gc_MissionsPurePursuit
                }

class gc_MissionsList(QDialog):
    def __init__(self,parent=None, whichfly = "whichfly"):
        super().__init__(parent)
        self.fly:str = whichfly
        self.ui = Ui_MissionsList()
        self.ui.setupUi(self)
        self.parent = parent
        self.setWindowTitle(f"{self.fly}的任务列表")
        self.comboBoxInit()
        #注册读取任务列表的回调函数
        self.parent.agent.set_readList_callback(self.readList_callback)
        self.ask_missionslist()
        #双击任务列表，显示任务详情
        self.ui.listWidget.itemDoubleClicked.connect(self.show_mission_data)
        
        self.ui.addpushButton.clicked.connect(self.addpushButton_clicked)
        self.ui.updateListButton.clicked.connect(self.ask_missionslist)
        self.ui.deleteLastButton.clicked.connect(self.deleteLastButton_clicked)
        self.ui.deleteSelButton.clicked.connect(self.deleteSelButton_clicked)
        self.ui.appendButton.clicked.connect(self.appendButton_clicked)
    
    def appendButton_clicked(self):
        missionsType = self.ui.comboBox.currentText()
        self.missionWindow = mission_table[missionsType](self,self.fly,index=self.ui.listWidget.count())
        self.missionWindow.show()

    def deleteSelButton_clicked(self):
        index = self.ui.listWidget.currentRow()
        self.parent.agent.publish_MS_deleteMission(self.fly,index)
        self.ask_missionslist()

    def deleteLastButton_clicked(self):
        index = self.ui.listWidget.count()-1
        self.parent.agent.publish_MS_deleteMission(self.fly,index)
        self.ask_missionslist()

    def comboBoxInit(self):
        for item in mission_table:
            self.ui.comboBox.addItem(item)

    def addpushButton_clicked(self):
        missionsType = self.ui.comboBox.currentText()
        self.missionWindow = mission_table[missionsType](self,self.fly,index=self.ui.listWidget.currentRow()+1)
        self.missionWindow.show()

    def ask_missionslist(self):
        self.parent.agent.ask_missionslist(self.fly)
        #展示waypoint的坐标点在地图上
    
    def readList_callback(self,client, userdata, msg):
        if self.fly in msg.topic:
            self.show_missionslist()

    #显示任务列表
    def show_missionslist(self):
        listWidegt = self.ui.listWidget
        currentTaskIndex = None
        #清空列表
        listWidegt.clear()
        # #添加任务列表
        # if self.fly == "fly1":
        #     for index, mission in enumerate(self.parent.agent.uav_mirror_1.missionsList):
        #         listWidegt.addItem(f"任务{index}: {mission['missionsType']}")
        #         currentTaskIndex = self.parent.agent.uav_mirror_1.missionsIndex
        # elif self.fly == "fly2":
        #     for index, mission in enumerate(self.parent.agent.uav_mirror_2.missionsList):
        #         listWidegt.addItem(f"任务{index}: {mission['missionsType']}")
        #         currentTaskIndex = self.parent.agent.uav_mirror_2.missionsIndex
        #找到mirror
        mirror = self.parent.agent.find_mirror_by_idstr(self.fly)
        if mirror is not None:
            #在地图上显示
            for index, mission in enumerate(mirror.missionsList):
                listWidegt.addItem(f"任务{index}: {mission['missionsType']}")
                # currentTaskIndex = mirror.missionsIndex
        
        #已经运行过的任务，颜色变灰有bug暂时不用
        # if currentTaskIndex is not None:
        #     for index in range(listWidegt.count()):
        #         item = listWidegt.item(index)
        #         if index <= currentTaskIndex:
        #             item.setForeground(QBrush(QColor(128, 128, 128)))
    
    def show_mission_data(self,item):
        #根据现在listWidget的选中项，显示任务数据
        #获取选中项的索引
        index = self.ui.listWidget.currentRow()
        #获取选中项的任务类型
        # if self.fly == "fly1":
        #     datasets = self.parent.agent.uav_mirror_1.missionsList[index]["datasets"]
        #     missionsType = self.parent.agent.uav_mirror_1.missionsList[index]["missionsType"]
        # elif self.fly == "fly2":
        #     datasets = self.parent.agent.uav_mirror_2.missionsList[index]["datasets"]
        #     missionsType = self.parent.agent.uav_mirror_2.missionsList[index]["missionsType"]
        #以信息弹窗的形式显示
        mirror = self.parent.agent.find_mirror_by_idstr(self.fly)
        if mirror is not None:
            datasets = mirror.missionsList[index]["datasets"]
            missionsType = mirror.missionsList[index]["missionsType"]
        self.ui.datasetsLabel.setText(f"任务类型: {missionsType}\n数据: {datasets}")

    
    def closeEvent(self, event):
        # 当窗口关闭时，启用父窗口的按钮
        if self.parent:
            if self.fly == "fly1":
                self.parent.ui.uav1msButton.setEnabled(True)
            elif self.fly == "fly2":
                self.parent.ui.uav2msButton.setEnabled(True)
        event.accept()        
            



        # self.ui.addpushButton.clicked.connect(self.pushButton_clicked)
        # self.ui.deletepushButton.clicked.connect(self.deletepushButton_clicked)
        # self.ui.listWidget.itemDoubleClicked.connect(self.show_item_index)
        #调用parent的代理的方法，获取任务列表
        # self.parent.agent.get_missionslist()
        # self.parent.agent.set_missionslist_callback(self.set_missionslist)
        # self.parent.agent.set_missionslist_update_callback(self.set_missionslist_update)